//
//  CreateOrderModel.swift
//
//  Created by eme on 2016/12/25
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol

class CreateOrderModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kCreateOrderPayidKey: String = "payid"
    internal let kCreateOrderOrderSumAmountKey: String = "orderSumAmount"
    internal let kCreateOrderYesmaiKey: String = "yesmai"
    internal let kCreateOrderStatusKey: String = "status"
    internal let kCreateOrderNomaiKey: String = "nomai"
    
    
    // MARK: 属性
    var payid: String
    var orderSumAmount: Double
    var yesmai: [YesmaiModel]
    var status: String
    var nomai: [NomaiModel]
    var paycouponid:String
    
    var nomaiHeight:CGFloat{
        get{
            var nimaiheight:CGFloat = 0.0
            for item  in self.nomai {
                nimaiheight = item.totalHeight
            }
            
            
            return nimaiheight
        }
    }
    
    
    // MARK: 实现ModelProtocol 协议， 解析json数据
    required  init?(json: JSON) {
        payid = json[kCreateOrderPayidKey].stringValue
        paycouponid = json["paycouponid"].stringValue
        orderSumAmount = json[kCreateOrderOrderSumAmountKey].doubleValue
        yesmai = []
        if let items = json[kCreateOrderYesmaiKey].array {
            for item in items {
                if let yesmaiModel = YesmaiModel(json: item){
                    yesmai.append(yesmaiModel)
                }
            }
        }
        status = json[kCreateOrderStatusKey].stringValue
        nomai = []
        if let items = json[kCreateOrderNomaiKey].array {
            for item in items {
                if let nomaiModel = NomaiModel(json: item){
                    nomai.append(nomaiModel)
                }
            }
        }
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(payid as AnyObject, forKey: kCreateOrderPayidKey)
        
        dictionary.updateValue(orderSumAmount as AnyObject, forKey: kCreateOrderOrderSumAmountKey)
        
        if yesmai.count > 0 {
            var temp: [AnyObject] = []
            for item in yesmai {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kCreateOrderYesmaiKey)
        }
        
        dictionary.updateValue(status as AnyObject, forKey: kCreateOrderStatusKey)
        
        if nomai.count > 0 {
            var temp: [AnyObject] = []
            for item in nomai {
                temp.append(item.dictionaryRepresentation() as AnyObject)
            }
            dictionary.updateValue(temp as AnyObject, forKey: kCreateOrderNomaiKey)
        }
        
        return dictionary
    }
    
    
}
